package com.yangjifang.mr.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * 理解mapreduce的核心是键值对  同样的键的数据会放在一次方法中调用
 * KEYIN 输入数据的key类型
 * VALUEIN 输入数据的value类型
 * KEYOUT 输出数据的key类型
 * VALUEOUT 输出数据的value类型
 */
public class WordCountMapper extends Mapper<LongWritable, Text,Text, IntWritable> {

    protected Text k = new Text();
    protected IntWritable v = new IntWritable(1);

    /**
     * 这里的值就是文本的一行
     * @param key
     * @param value
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        //获取一行
        String line = value.toString();

        //切割单词
        String[] words = line.split(" ");

        //循环写出
        for(String word: words){
            k.set(word);
            //同样的键会被reduce一次调用
            context.write(k,v);
        }
    }

}
